package com.pastock.zookeepermanager;

import java.util.List;
import java.util.Map;

import org.apache.zookeeper.AsyncCallback.StatCallback;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.springframework.stereotype.Component;
@Component
public class ZookeeperDataMonitor implements Watcher, StatCallback {

	ZooKeeper zk;

    String znode;

    Watcher chainedWatcher;

    boolean dead;

    ZookeeperDataMonitorListener listener;

    byte prevData[];
    
    public void monitor(ZooKeeper zk, String znode, Watcher chainedWatcher,
    		ZookeeperDataMonitorListener listener) {
        this.zk = zk;
        this.znode = znode;
        this.chainedWatcher = chainedWatcher;
        this.listener = listener;
        zk.exists(znode, true, this, null);
    }
    
    /**
     * Other classes use the DataMonitor by implementing this method
     */
    public interface ZookeeperDataMonitorListener {
        void exists(byte data[]);
        void closing(int rc);
    }
    
	public void process(WatchedEvent event) {
		System.out.println("事件状态:" + event.getState() +",事件类型:" +  event.getType() +",事件涉及路径:" + event.getPath());
    	if(event.getState() == KeeperState.Expired){//session expired connection
    		listener.closing(0);
    	}else{
	    	//先获取到所有的系统节点
	    	try {
				List<String> systemConfigNameList = zk.getChildren(znode, true);
				for (String systemConfigName : systemConfigNameList) {
					if (event.getType()==EventType.None && null==event.getPath()){
						//遍历子节点数据
						Map<String,String> zkConfigMap = ZookeeperConfigrutionConstant.getZookeeperConfigrutionConstantMap();
						byte[] data = zk.getData(znode+"/"+systemConfigName, true, null);
	                	zkConfigMap.put(znode+"/"+systemConfigName, new String(data));
	                	System.out.println("name:"+znode+"/"+systemConfigName+"  valeu:"+new String(data));
						System.out.println("建立链接时获取指定路径下节点(并对其节点及子节点设置监听)，子节点->" + systemConfigName);
					}else{
						//子节点发送改变
		                if (event.getType() == EventType.NodeChildrenChanged){
		                     try {
		                    	  //修改变化的节点
		                    	  String path = event.getPath();
		                    	  List<String> children = zk.getChildren(path, true);
		                    	  for (String childrenNodeName : children) {
									byte[] ChangedChildrenNodedData = zk.getData(path+"/"+childrenNodeName, true, null);
									Map<String,String> zkConfigMap = ZookeeperConfigrutionConstant.getZookeeperConfigrutionConstantMap();
									zkConfigMap.put(path+"/"+childrenNodeName, new String(ChangedChildrenNodedData));
									System.out.println("childrenNodeName:"+childrenNodeName+" ChangedChildrenNodedData:"+new String(ChangedChildrenNodedData));
								}
		                          System.out.println("指定节点下节点发生变化重新获取子并设置监听器:" + children);
		                      } catch (KeeperException e) {
		                          e.printStackTrace();
		                      } catch (InterruptedException e) {
		                          e.printStackTrace();
		                      }
		                  }
		                if (event.getType() == EventType.NodeDataChanged) {
							System.out.println("节点数据发生改变");
							 //修改变化的节点
	                    	 String path = event.getPath();
	                    	 byte[] ChangedChildrenNodedData = zk.getData(path, true, null);
	                    	 System.out.println(new String(ChangedChildrenNodedData));
						}
		                if(event.getType() == EventType.NodeDeleted){
		                	System.out.println("节点删除");
		                }
		                if(event.getType() == EventType.NodeCreated){
		                	System.out.println("节点创建");
		                }
					}
				}
			} catch (KeeperException e1) {
				e1.printStackTrace();
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
    	}
	}

	public void processResult(int arg0, String arg1, Object arg2, Stat arg3) {
		System.out.println("processResult");
	}
	
	/*
	public void process(WatchedEvent event) {
		System.out.println("事件状态:" + event.getState() +",事件类型:" +  event.getType() +",事件涉及路径:" + event.getPath());
    	if(event.getState() == KeeperState.Expired){//session expired connection
    		listener.closing(0);
    	}else{
	    	//先获取到所有的系统节点
	    	try {
				List<String> systemConfigNameList = zk.getChildren(znode, true);
				for (String systemConfigName : systemConfigNameList) {
					if (event.getType()==EventType.None && null==event.getPath()){
		                List<String> childrenNameList = zk.getChildren(znode+"/"+systemConfigName, true);
		                for (String childrenName : childrenNameList) {
		                	//初始化所有配置到内存中
		                	Map<String,String> zkConfigMap = ZookeeperConfigrutionConstant.getZookeeperConfigrutionConstantMap();
		                	byte[] data = zk.getData(znode+"/"+systemConfigName+"/"+childrenName, true, null);
		                	zkConfigMap.put(znode+"/"+systemConfigName+"/"+childrenName, new String(data));
		                	System.out.println("name:"+znode+"/"+systemConfigName+"/"+childrenName+"  valeu:"+new String(data));
						}
						System.out.println("建立链接时获取指定路径下节点(并对其节点及子节点设置监听)，子节点->" + childrenNameList);
					}else{
						//子节点发送改变
		                if (event.getType() == EventType.NodeChildrenChanged){
		                     try {
		                    	  //修改变化的节点
		                    	  String path = event.getPath();
		                    	  List<String> children = zk.getChildren(path, true);
		                    	  for (String childrenNodeName : children) {
									byte[] ChangedChildrenNodedData = zk.getData(path+"/"+childrenNodeName, true, null);
									Map<String,String> zkConfigMap = ZookeeperConfigrutionConstant.getZookeeperConfigrutionConstantMap();
									zkConfigMap.put(path+"/"+childrenNodeName, new String(ChangedChildrenNodedData));
									System.out.println("childrenNodeName:"+childrenNodeName+" ChangedChildrenNodedData:"+new String(ChangedChildrenNodedData));
								}
		                          System.out.println("指定节点下节点发生变化重新获取子并设置监听器:" + children);
		                      } catch (KeeperException e) {
		                          e.printStackTrace();
		                      } catch (InterruptedException e) {
		                          e.printStackTrace();
		                      }
		                  }
		                if (event.getType() == EventType.NodeDataChanged) {
							System.out.println("节点数据发生改变");
							 //修改变化的节点
	                    	 String path = event.getPath();
	                    	 byte[] ChangedChildrenNodedData = zk.getData(path, true, null);
	                    	 System.out.println(new String(ChangedChildrenNodedData));
						}
		                if(event.getType() == EventType.NodeDeleted){
		                	System.out.println("节点删除");
		                }
		                if(event.getType() == EventType.NodeCreated){
		                	System.out.println("节点创建");
		                }
					}
				}
			} catch (KeeperException e1) {
				e1.printStackTrace();
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
    	}
    	
	}*/
}
